package multitionPattern;

import java.util.HashMap;
import java.util.Map;

/**
 * 固定数量多例模式
 */
public class FixedMultiton {
    private static final int INSTANCE_COUNT = 3;
    private static final Map<Integer, FixedMultiton> instances = new HashMap<>();
    
    static {
        // 预先创建所有实例
        for (int i = 1; i <= INSTANCE_COUNT; i++) {
            instances.put(i, new FixedMultiton("Instance-" + i));
        }
    }
    
    private final String name;
    
    private FixedMultiton(String name) {
        this.name = name;
        System.out.println("创建多例实例: " + name);
    }
    
    public static FixedMultiton getInstance(int key) {
        if (key < 1 || key > INSTANCE_COUNT) {
            throw new IllegalArgumentException("键值必须在 1-" + INSTANCE_COUNT + " 之间");
        }
        return instances.get(key);
    }
    
    public String getName() {
        return name;
    }
    
    public void doSomething() {
        System.out.println(name + " 正在执行操作...");
    }
}